package com.util.baidu;


import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.baidu.yun.push.auth.PushKeyPair;
import com.baidu.yun.push.client.BaiduPushClient;
import com.baidu.yun.push.constants.BaiduPushConstants;
import com.baidu.yun.push.model.PushMsgToSingleDeviceRequest;
import com.baidu.yun.push.model.PushMsgToSingleDeviceResponse;
import org.apache.log4j.Logger;

/**
 * Created by reeveslee on 16/5/14.
 */
public class BaiduPush {
    private static final Logger LOGGER = Logger.getLogger(BaiduPush.class);

    private static String apiKeyIOS = "ZcuNzoaQFOsbRWBpUQUEodAC";
    private static String secretKeyIOS = "wlqUmLoEiUgczOg7YIn6bGKLka2ZhpK2";
    private static String apiKeyAndroid = "c6DQrPNa7vdyYTqniGGE9XQn";
    private static String secretKeyAndroid = "ldQGqb7ihu8cTP9KgGSWlODAsW2mHNS1";

    /**
     * IOS 平台百度云推送
     *
     * @param baiduId
     * @param msg
     * @return
     */
    public static boolean pushMsgToSingleDeviceIOS(String baiduId, MsgModelIOS msg) {
        PushKeyPair pair = new PushKeyPair(apiKeyIOS, secretKeyIOS);
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest().
                    addChannelId(baiduId).
                    addMsgExpires(new Integer(3600)).
                    addMessageType(1).
                    addMessage(msg.toJsonString()).
                    addDeployStatus(2).
                    addDeviceType(4);
            PushMsgToSingleDeviceResponse response = pushClient.pushMsgToSingleDevice(request);
        } catch (Exception e) {
            //LOGGER.error(e);
        }

        return true;
    }


    /**
     * Android 平台百度云推送
     *
     * @param baiduId
     * @param msg
     * @return
     */
    public static boolean pushMsgToSingleDeviceAndroid(String baiduId, MsgModelAndroid msg) {
        PushKeyPair pair = new PushKeyPair(apiKeyAndroid, secretKeyAndroid);
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest().
                    addChannelId(baiduId).
                    addMsgExpires(new Integer(3600)).
                    addMessageType(1).
                    addMessage(msg.toJsonString()).
                    addDeviceType(3);
            PushMsgToSingleDeviceResponse response = pushClient.pushMsgToSingleDevice(request);
        } catch (Exception e) {
            //LOGGER.error(e);
        }

        return true;
    }

    public static void main(String[] args){
//        MsgModelAndroid msgModelAndroid = new MsgModelAndroid();
//        msgModelAndroid.setDescription("测试服务器发送推送");
//        msgModelAndroid.custom_content.put("type", "feedReply");
//        msgModelAndroid.custom_content.put("feedId", 23);
//        BaiduPush.pushMsgToSingleDeviceAndroid("5393899735273810080", msgModelAndroid);
        MsgModelIOS msgModelIOS = new MsgModelIOS();
        msgModelIOS.setAlertStr("hahah");
        msgModelIOS.put("type","feedReply");
        msgModelIOS.put("feedId",23);
        BaiduPush.pushMsgToSingleDeviceIOS("5563782694801144723",msgModelIOS);
    }

}
